package view;

import java.awt.EventQueue;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import controller.CreateBill;
import controller.CreateEnsured;
import model.entityInclude.ReinsuranceBilling;
import model.entityInclude.ReinsuranceEnsured;
import baseclass.ErrorPage;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.net.ServerSocket;

/**
 * @author 50123
 * @category 火險再保帳單切檔
 * 
 * */
public class Main
{

    private static JFrame frame;
    private JFileChooser fc=new JFileChooser();
    private File insertFile=null;
    private int ret=1;
    @SuppressWarnings("unused")
    private static ServerSocket srvSocket=null;
    private static final int srvPort=5566; // 防止重複開啟

    /**
     * Launch the application.
     */
    public static void main(String[] args)
    {
	EventQueue.invokeLater(new Runnable()
	{
	    @SuppressWarnings("static-access")
	    public void run()
	    {
		try
		{
		    Main window=new Main();
		    window.checkSingleInstance();
		    window.frame.setVisible(true);
		}
		catch (Exception e)
		{
		    e.printStackTrace();
		}
	    }
	});
    }

    /**
     * Create the application.
     */
    public Main()
    {
	initialize();
    }

    protected void checkSingleInstance()
    {
	try
	{
	    srvSocket=new ServerSocket(srvPort); // 啟用ServerSocket，用以控制只能開啟一個程序
	}
	catch (Exception ex)
	{
	    if(ex.getMessage().indexOf("Address already in use: JVM_Bind")>=0)
	    {
		JOptionPane.showMessageDialog(null,"該程式已啟動","提示",JOptionPane.OK_OPTION);
		System.exit(0);

	    }
	}
    }

    /**
     * Initialize the contents of the frame.
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    private void initialize()
    {
	frame=new JFrame();
	frame.setTitle("火險再保轉帳單");
	frame.setForeground(Color.BLACK);
	frame.setBounds(100,100,266,131);
	frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	frame.getContentPane().setLayout(null);
	frame.setResizable(false);
	frame.setLocationRelativeTo(null);
	JButton trbt=new JButton("轉檔");
	trbt.setBounds(132,57,101,29);
	frame.getContentPane().add(trbt);
	final JComboBox comboBox=new JComboBox();
	comboBox.setFont(new Font("PMingLiU",Font.PLAIN,15));
	comboBox.setForeground(Color.GRAY);
	comboBox.setModel(new DefaultComboBoxModel(new String[] { "請選擇", "承保帳單", "批改帳單" }));
	comboBox.setBounds(14,13,219,27);
	frame.getContentPane().add(comboBox);

	JButton opbt=new JButton("開啟");
	opbt.setBounds(14,57,101,29);
	frame.getContentPane().add(opbt);
	trbt.addActionListener(new ActionListener()
	{
	    public void actionPerformed(ActionEvent e)
	    {
		if(ret==JFileChooser.APPROVE_OPTION)
		{
		    try
		    {

			if(comboBox.getSelectedIndex()==0)
			{
			    JOptionPane.showMessageDialog(null,"請選擇產生檔案方式");
			}
			if(comboBox.getSelectedIndex()==1)
			{
			    ReinsuranceBilling bills=new ReinsuranceBilling();
			    CreateBill create=new CreateBill();
			    create.setBills(bills.inFile(insertFile.getAbsolutePath()));
			    new Thread(create).start();
			}
			if(comboBox.getSelectedIndex()==2)
			{
			    ReinsuranceEnsured bills=new ReinsuranceEnsured();
			    CreateEnsured create=new CreateEnsured();
			    create.setBills(bills.inFile(insertFile.getAbsolutePath()));
			    new Thread(create).start();
			}
		    }
		    catch (Exception e1)
		    {
			new ErrorPage(e1);
		    }

		}
		else
		{
		    JOptionPane.showMessageDialog(null,"無匯入檔案");
		}

	    }
	});
	opbt.addActionListener(new ActionListener()
	{
	    public void actionPerformed(ActionEvent arg0)
	    {
		fc.setCurrentDirectory(new java.io.File("C:\\workf"));
		ret=fc.showOpenDialog(null);
		insertFile=fc.getSelectedFile();

	    }
	});
    }
}
